package com.cardone.platform.authority.util;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.cardone.common.Characters;
import com.cardone.context.Contexts;
import com.cardone.platform.authority.dto.PermissionDto;
import com.cardone.platform.configuration.dto.SiteDto;
import com.cardone.platform.configuration.util.SiteUtils;
import com.cardone.platform.usercenter.dto.UserDto;
import com.cardone.platform.usercenter.util.UserUtils;
import com.google.common.collect.Lists;

/**
 * 许可工具类
 *
 * @author yaohaitao
 *
 */
public class PermissionUtils {
	/**
	 * 是否有权限
	 *
	 * @param permissionList
	 *          授权集合
	 * @param typeCode
	 *          类别代码
	 * @param code
	 *          代码
	 * @return 是否有权限
	 */
	public static boolean isAuthority(final List<PermissionDto> permissionList, final String typeCode, final String code) {
		if (CollectionUtils.isEmpty(permissionList)) {
			return false;
		}

		for (final PermissionDto permission : permissionList) {
			if (!StringUtils.equals(permission.getTypeCode(), typeCode)) {
				continue;
			}

			if (StringUtils.equals(permission.getCode(), code)) {
				return true;
			}
		}

		return false;
	}

	/**
	 * 是否有权限
	 *
	 * @param block
	 *          区块
	 *
	 * @return 是否有权限
	 */
	public static boolean isBlockAuthority(final String block) {
		final UserDto loginUser = UserUtils.findForLogin();

		return PermissionUtils.isBlockAuthority(loginUser, block);
	}

	/**
	 * 是否有权限
	 *
	 * @param loginUser
	 *          用户对象
	 * @param block
	 *          路径
	 * @return 是否有权限
	 */
	public static boolean isBlockAuthority(final UserDto loginUser, final String block) {
		return PermissionUtils.isAuthority(loginUser.getPermissionList(), Contexts.block.name(), block);
	}

	/**
	 * 是否有权限
	 *
	 * @return 是否有权限
	 */
	public static boolean isUrlAuthority() {
		final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

		return PermissionUtils.isUrlAuthority(request);
	}

	/**
	 * 是否有权限
	 *
	 * @param request
	 *          HttpServletRequest
	 * @return 是否有权限
	 */
	public static boolean isUrlAuthority(final HttpServletRequest request) {
		final UserDto loginUser = UserUtils.findForLogin(request);

		return PermissionUtils.isUrlAuthority(loginUser, request.getServletPath());
	}

	/**
	 * 是否有权限
	 *
	 * @param url
	 *          路径
	 *
	 * @return 是否有权限
	 */
	public static boolean isUrlAuthority(final String url) {
		final UserDto loginUser = UserUtils.findForLogin();

		return PermissionUtils.isUrlAuthority(loginUser, url);
	}

	/**
	 * 是否有权限
	 *
	 * @param loginUser
	 *          用户对象
	 * @param url
	 *          路径
	 * @return 是否有权限
	 */
	public static boolean isUrlAuthority(final UserDto loginUser, final String url) {
		final SiteDto contextSite = SiteUtils.getContextSite();

		if (CollectionUtils.isEmpty(contextSite.getUrlWhiteList())) {
			return true;
		}

		if (com.cardone.common.util.StringUtils.matchList(contextSite.getUrlWhiteList(), url)) {
			return true;
		}

		final String permissionCode = com.cardone.common.util.StringUtils.getPathForMatch(contextSite.getUrlBlackList(), url);

		if (StringUtils.isBlank(permissionCode)) {
			return true;
		}

		if (StringUtils.isBlank(loginUser.getLoginKey())) {
			return false;
		}

		return PermissionUtils.isAuthority(loginUser.getPermissionList(), Contexts.urlBlackList.name(), permissionCode);
	}

	/**
	 * 是否有权限
	 *
	 * @param url
	 *          路径
	 *
	 * @return 是否有权限
	 */
	public static boolean isUrlsAuthority(final String urls) {
		final List<String> urlList = Lists.newArrayList(StringUtils.split(urls, Characters.comma.stringValue()));

		if (CollectionUtils.isEmpty(urlList)) {
			return false;
		}

		final UserDto loginUser = UserUtils.findForLogin();

		for (final String url : urlList) {
			if (PermissionUtils.isUrlAuthority(loginUser, url)) {
				return true;
			}
		}

		return false;
	}
}
